<?php
$this->title = "文章列表";
//var_dump($articles[0]);
?>
<div class="main">
	<div class="header">文章列表</div>
	<div class="oper">
		<a href="<?=ROOT_URL?>article/create" class='btn btn-primary'>增加新文章</a>
	</div>
	<table class="mytable table table-bordered table-striped table-hover" id="article" width="150%">
		<thead>
			<tr>
				<th width="20">序号</th>
				<th>标题</th>
				<th width="30">作者</th>
				<th width="60">文章发布人</th>
				<th width="30">修改日期</th>
				<th width="30">提交日期</th>
				<th width="30">发布日期</th>
				<th width="60">最后审核人</th>
				<th width="50">审核日期</th>
				<th width="60">紧急重要度</th>
				<th width="50">栏目</th>
				<th width="60">用户组</th>
				<th width="50">文章状态</th>
				<th width="60">操作</th>
			</tr>
		</thead>
		<tbody>
			<?php
		 	//var_dump($users);
		 	foreach ($articles as $key => $value) {
		 		echo "<tr>";
		 		echo "<td>".$value['id']."</td>";
		 		echo "<td><a href='/front/Home/article/article?id=".$value['id']."' target='_blank'>".$value['title']."</a></td>";
		 		echo "<td>".$value['author']."</td>";
		 		echo "<td>".$value['postername']."</td>";
		 		echo "<td>".$value['updatetime']."</td>";
		 		echo "<td>".$value['submittime']."</td>";
		 		echo "<td>".$value['publishtime']."</td>";
		 		echo "<td>".$value['auditorname']."</td>";
		 		echo "<td>".$value['audittime']."</td>";
		 		switch ($value['priority']) {
		 			case '一般':
		 				echo "<td style='color:#4cae4c;'>".$value['priority']."</td>";
		 				break;
		 			case '紧急':
		 				echo "<td style='color:#F50C06;'>".$value['priority']."</td>";
		 				break;
		 			case '重要':
		 				echo "<td style='color:#ffab1a;'>".$value['priority']."</td>";
		 				break;
		 			default:
		 				echo "<td>".$value['priority']."</td>";
		 				break;
		 		}
		 		
		 		echo "<td>".$value['category']."</td>";
		 		echo "<td>"."</td>";
		 		switch ($value['status']) {
		 			case '1':
		 				echo "<td>编辑中</td>";
		 				echo "<td>";
		 				if($_SESSION['cms_usertype'] == 4 || $_SESSION['cms_userid'] == $value['posterid'])
		 				{
		 				echo "<a href='javascript:;' class='subaudit'>提交审核</a>";
						echo "<br>";
		 				echo "<a href='javascript:;' class='a_edit'>修改</a>";
						echo "<br>";
		 				echo "<a href='javascript:;' class='a_del'>删除</a>";
		 				echo "<input class='aid' type='hidden' value='".$value['id']."'>";
		 				}
		 				echo "</td>";
		 				break;
		 			case '2':
					    echo "<td>审核中</td>";
					    echo "<td>";
					    if($_SESSION['cms_usertype'] == 4 || $_SESSION['cms_userid'] == $value['posterid'] || $_SESSION['cms_userid'] == $value['auditorid'])
					    {
					    	if($_SESSION['cms_userid'] == $value['auditorid']){
								echo "<a href='javascript:;' class='doaudit'>审核</a>";
								echo "<br>";
							}
					    	echo "<a href='javascript:;' class='delaudit'>撤回</a>";
							echo "<br>";
					    	echo "<a href='javascript:;' class='a_del'>删除</a>";
					    }
					    
					    echo "<a href='javascript:;' class='audithistory'>审核历史</a>";
					    echo "<input class='aid' type='hidden' value='".$value['id']."'>";
					    echo "</td>";
						break;
					case '3':
						echo "<td>已发布</td>";
						echo "<td>";
						if($_SESSION['cms_usertype'] == 4 || $_SESSION['cms_userid'] == $value['posterid'] || $_SESSION['cms_userid'] == $value['auditorid'])
						{
							echo "<a href='javascript:;' class='delaudit'>撤回</a>";
							echo "<br>";
							echo "<a href='javascript:;' class='a_del'>删除</a>";
						}
						echo "<br>";
						echo "<a href='javascript:;' class='audithistory'>审核历史</a>";
						echo "<input class='aid' type='hidden' value='".$value['id']."'>";
						echo "</td>";
						break;
					
		 			default:
		 				echo "<td></td><td></td>";
		 				break;
		 		}
		 		//echo "<td></td>";
		 		echo "</tr>";
		 	}
		 	?>
		</tbody>
	</table>
<div>
<div class="modal" id="subauditModal" tabindex="-1" role="dialog" aria-labelledby="subauditModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
    <form id="subaudit-form" action="<?=ROOT_URL?>article/applyaudit" method="post">
      <div class="modal-body">
      	
      		<input id="subaudithidden" type="hidden" value="" name="id">
          <div class="form-group">
            下一位审核人：
            <select id="selsubauditor" name="auditorid" class="form-control" style="display:inline;width:50%">

            </select>
        	
          </div>
		  <div class="form-group">
			  审核人手机号：
			  <input type="text" class="form-control" name="cellphone" style="display:inline;width:50%" placeholder="审核人手机号码">
		  </div>
      </div>
      <div class="modal-footer">
        <input id="subaudit-btn" type="button" value="提交" class="btn btn-primary">
        <input type="button" value="取消" class="btn btn-danger" data-dismiss="modal">
      </div>
      </form>
    
  </div>
</div> 	
</div>

<div class="modal" id="audithistoryModal" tabindex="-1" role="dialog" aria-labelledby="audithistoryModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-body">
      	<ul id="ul_audithistory">
      	</ul>
      </div>
      <div class="modal-footer">
        <input type="button" value="取消" class="btn btn-danger" data-dismiss="modal">
      </div>
    </div>
  </div>
</div>

<div class="modal" id="doauditModal" tabindex="-1" role="dialog" aria-labelledby="">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
    	<form id="doaudit-form" action="<?=ROOT_URL?>article/doaudit" class="form-horizontal" method="post">
      		<div class="modal-body">
      	
      		<input id="doaudithidden" type="hidden" value="" name="id">
      		<div class="form-group">
         		<label class="control-label col-sm-3">审核结果：</label>
         		<div class="col-sm-6">
					<div class="radio col-sm-6">
					  <label>
					    <input type="radio" class="" name="auditresult" value="1" checked>审核通过
					  </label>
					</div>
					<div class="radio col-sm-6">
					  <label>
					    <input type="radio" class="" name="auditresult" value="0">审核不通过
					  </label>
					</div>
				</div>
          	</div>
          	<div class="form-group">
				<label class="col-sm-3 control-label">附言：</label>
				<div class="col-sm-6">
					<textarea class="form-control" name="comment" rows=3></textarea>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-3 control-label">手机号码：</label>
				<div class="col-sm-6">
					<input type="text" class="form-control" name="cellphone" placeholder="要通知的手机号码">
				</div>
			</div>
	        <div class="form-group">
	        	<label class="col-sm-3 control-label">下一位审核人：</label>
				<div class="col-sm-6">
					<select id="seldoauditor" class="form-control" name="nextauditor">
						
					</select>
				</div>
	        </div>
      		</div>
		      <div class="modal-footer">
		        <input id="doaudit-btn" type="button" value="提交" class="btn btn-primary">
		        <input type="button" value="取消" class="btn btn-danger" data-dismiss="modal">
		      </div>
		</form>
    </div>
  </div>
</div> 
<script type="text/javascript" src="../web/js/jquery.form.js"></script>
<script type="text/javascript">
$("#article").dataTable({
    //"lengthChange": false,
    "dom": 't<"col-sm-5"i>p',
    //"ordering": false,
    "sPaginationType": "full_numbers",
    "oLanguage": {
        "sInfo": "共_TOTAL_条数据，展示_START_到_END_条数据",
        "sInfoEmpty": "无数据",
        "sInfoFiltered": "(从_MAX_条数据中的查询)",
        "sLengthMenu": "每页显示 _MENU_ 条数据",
        "sSearch":  "查找 _INPUT_ ",
        "sZeroRecords": " ",
        "oPaginate":{
            "sPrevious":"上一页",
            "sNext":"下一页",
            "sLast":"尾页",
            "sFirst":"首页"
        },
    },
    "iDisplayLength":10,
    "columnDefs": [ {
      "targets": [10, 11,12,13 ],
      "orderable": false
    } ],
    "order": [[4,'desc']]                  
});

$(document).on("click","a.subaudit",function(){
	var id = $(this).siblings("input.aid").val();
	//alert(id);
	$("#subaudithidden").val(id);
	$.getJSON("<?=ROOT_URL?>article/getauditor",function(data){
		//alert(data.length);
		var str = "";
		for (var i = 0; i < data.length; i++) {
			str += "<option value='"+data[i]['id']+"'>"+data[i]['username']+"</option>";
		};
		$("#selsubauditor").html(str);
		$("#subauditModal").modal();
	});
	
});

$(document).on("click","a.delaudit",function(){
	var id = $(this).siblings("input.aid").val();
	//alert(id);
	if(confirm("撤回后您要重新提交审核并通过后才能发布，您确定要撤回吗？")){
		$.post("<?=ROOT_URL?>article/delaudit",{id:id},function(data){
			window.location.reload();
		});
	}
});

$(document).on("click","a.a_del",function(){
	var id = $(this).siblings("input.aid").val();
	//alert(id);
	if(confirm("您确定要删除这篇文章吗？")){
		$.post("<?=ROOT_URL?>article/delarticle",{id:id},function(data){
			window.location.reload();
		});
	}
});

$(document).on("click","a.audithistory",function(){
	var id = $(this).siblings("input.aid").val();
	//alert(id);
	$.getJSON("<?=ROOT_URL?>article/audithistory",{id:id},function(data){
		var str = "";
		var length = data.length;
		for (var i = 0; i < length; i++) {
			str += "<li>"
			str += "<span class='ah-time'>"+data[i].audittime+"</span>";
			str += "<span class='ah-auditor'>"+data[i].username+"</span>";
			switch(data[i].status)
			{
				case "0":
					str += "<span class='ah-content'>接受了申请</span>";
					break;
				case "1":
					str += "<span class='ah-content'>通过申请</span>";
					break;
				case "2":
					str += "<span class='ah-content'>驳回了申请</span>";
					break;
				default:

			}
			str += "</li>";
			if(data[i].comment != "")
			{
				str += "<li class='ah-comment'>附言："+data[i].comment+"</li>";
			}

		};
		$("#ul_audithistory").html(str);
		$("#audithistoryModal").modal();
	});

});

$(document).on("click","a.doaudit",function(){
	var id = $(this).siblings("input.aid").val();
	//alert(id);
	$("#doaudithidden").val(id);
	$.getJSON("<?=ROOT_URL?>article/getauditor",function(data){
		//alert(data.length);
		var str = "";
		str += "<option value=''>无需审核，直接发布</option>";
		for (var i = 0; i < data.length; i++) {
			str += "<option value='"+data[i]['id']+"'>"+data[i]['username']+"</option>";
		};
		$("#seldoauditor").html(str);
		$("#doauditModal").modal();
	});
	
});

$(document).on("click","a.a_edit",function(){
	var id = $(this).siblings("input.aid").val();
	//alert(id);
	window.location.href = "<?=ROOT_URL?>article/editarticle&id="+id;
});

$("#doaudit-btn").click(function(){
	$("#doaudit-form").ajaxSubmit({
		success:function(){
			window.location.reload();
		}
	});
});

$("#subaudit-btn").click(function(){
	$("#subaudit-form").ajaxSubmit({
		success:function(){
			window.location.reload();
		}
	});
});

</script>

